﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;
using TierApplication;
using OrderMgr.SysSetting;

namespace OrderMgr.Rpt
{
    public partial class frmRptStockDetail : TierService.frmTemplate
    {
        public string _OrderID;
        public string _ProductID = "";
        public DateTime _StartDate;
        public DateTime _EndDate;
        public string _ProductType;
        public string _DepotID;

        public frmRptStockDetail()
        {
            InitializeComponent();
            this.Text = "商品出入库明细";
        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtProductType, C_DMB.GetListByType(C_DMB.DType.ProductType.ToString()), "CodeID", "CodeName");
            this.txtOrderDateStart.DateTime = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-01"));
            this.txtOrderDateEnd.DateTime = DateTime.Now.Date;
            this.ckbDepotID.Checked = true;

            if (_ProductID != "")
            {
                this.txtOrderDateStart.DateTime = _StartDate;
                this.txtOrderDateEnd.DateTime = _EndDate;
                this.txtDepotID.Visible = true;
                this.txtDepotID.EditValue = _DepotID;
                this.ckbDepotID.Visible = true;
                this.ckbProductType.Visible = false;
                this.txtProductType.Visible = false;
                btnSearch_Click(null, null);
            }
        }

        private void frmRptStockDetail_Load(object sender, EventArgs e)
        {
            initForm();

        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string sStartDate = txtOrderDateStart.DateTime.ToString("yyyyMMdd");
            string sEndDate = txtOrderDateEnd.DateTime.ToString("yyyyMMdd");
            string sProductType = (string)txtProductType.EditValue;
            string sDepotID = (string)txtDepotID.EditValue;
            string sProductID = _ProductID;
            DataSet dsRptStock = T_Product.GetRptStockDetail(sStartDate, sEndDate, sProductType, sDepotID, sProductID);
            gcOrder.DataSource = dsRptStock.Tables[0];
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            TierService.Utility.DataExportToExcel(gvOrder, "订单信息");
        }



        private void gvOrder_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            string sID = dr["ProductID"].ToString();
            T_Product mProduct = new T_Product();
            mProduct.GetModel(sID);
            string sProductType = mProduct.ProductProp;

            frmProductDetail fDetail = new frmProductDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Query;
            if (sProductType == "1")
                fDetail.gProductType = Utility.ProductType.MProduct;
            else
                fDetail.gProductType = Utility.ProductType.Product;

            fDetail._ProductID = sID;
            fDetail.ShowDialog();

        }

        private void gvOrder_DoubleClick(object sender, EventArgs e)
        {
            if (gFuncType == Utility.FuncType.Select)
            {
                if (gvOrder.RowCount < 1) return;
                DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
                _OrderID = dr["OrderID"].ToString();
                DialogResult = System.Windows.Forms.DialogResult.OK;

            }
            else { btnQuery_Click(null, null); }

        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void gvOrder_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                DataRow dr = gvOrder.GetDataRow(e.RowHandle);
                string OrderType;
                OrderType = dr["OrderTypeName"].ToString();
                if (OrderType == "期末结存")
                {
                    e.Appearance.ForeColor = Color.Red;
                }
                if (OrderType == "期初结存")
                {
                    e.Appearance.ForeColor = Color.Green;
                }

            }
        }

        private void btnQueryOrder_Click(object sender, EventArgs e)
        {
            if (gvOrder.RowCount < 1) return;
            DataRow dr = gvOrder.GetDataRow(gvOrder.FocusedRowHandle);
            _OrderID = dr["OrderID"].ToString();
            if (_OrderID != "")
            {
                OrderFunc.QueryOrder(_OrderID);
            }
        }
    }
}